package leetcode;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class LeetcodeMain83 {
    public void moveZeroes(int[] nums) {
        if (nums==null){
            return;
        }
        int j=0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]!=0){
                nums[j++]=nums[i];
            }
        }
        for (int i = j; i < nums.length; i++) {
            nums[i]=0;
        }
    }

    public static void main(String[] args) {
        LeetcodeMain83 main83=new LeetcodeMain83();
        main83.moveZeroes(new int[]{0,1,0,3,12});
    }
    public int findDuplicate1(int[] nums) {
        int length=nums.length;
        Arrays.sort(nums);
        int num=nums[0];
        for (int i = 1; i < length; i++) {
            num=num^nums[i];
            if (num==0){
                return nums[i];
            }
            num=nums[i];
        }
        return num;
    }
    public static void main3(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNext()){
            int num=in.nextInt();
            in.nextLine();
            String st=in.nextLine();
            int num1=0;
            int num2=0;
            String[] arr=st.split(" ");
            Stack<String> stack=new Stack<>();
            for (int i = 0; i < arr.length; i++) {
                switch (arr[i]){
                    case "+":
                        num2=Integer.parseInt(stack.pop());
                        num1=Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1+num2));
                        break;
                    case "-":
                        num2=Integer.parseInt(stack.pop());
                        num1=Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1-num2));
                        break;
                    case "*":
                        num2=Integer.parseInt(stack.pop());
                        num1=Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1*num2));
                        break;
                    case "/":
                        num2=Integer.parseInt(stack.pop());
                        num1=Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1/num2));
                        break;
                    default:
                        stack.push(arr[i]);
                        break;
                }
            }
            System.out.println(Integer.parseInt(stack.pop()));
        }
    }
//    public int findDuplicate(int[] nums) {
//
//    }
    public static void main2(String[] args){
        Scanner in=new Scanner(System.in);
        System.out.println(Long.MAX_VALUE);
        while(in.hasNext()){
            int num=in.nextInt();
            long[] arr=new long[num];
            for(int i=0;i<num;i++){
                arr[i]=in.nextInt();
            }
            int count=0;

            for(int i=1;i<num-1;i++){
                if(isRight(arr,i)&&isLeft(arr,i)){
                    count++;
                }
            }
            System.out.println(count);
        }
    }
    public static boolean isRight(long[] arr, int start){
        for(int i=0;i<start;i++){
            if(arr[i]>arr[start]){
                return true;
            }
        }
        return false;
    }
    public static boolean isLeft(long[] arr, int start){
        for(int i=start+1;i<arr.length;i++){
            if(arr[i]>arr[start]){
                return true;
            }
        }
        return false;
    }
    public int myAtoi(String s) {
        s=s.trim();
        if (s.length()==0){
            return 0;
        }
        int length=s.length();
        if (!Character.isDigit(s.charAt(0))&&s.charAt(0)!='+'&&s.charAt(0)!='-'){
            return 0;
        }
        long sum=0;
        boolean flag = s.charAt(0) == '-';
        int i=Character.isDigit(s.charAt(0))?0:1;
        for (; i < length&&Character.isDigit(s.charAt(i)); i++) {
           sum=sum*10+s.charAt(i)-'0';
           if (!flag&&sum>Integer.MAX_VALUE){
               sum=Integer.MAX_VALUE;
               break;
           }
            if (flag&&sum*-1>Integer.MIN_VALUE){
                sum=Integer.MIN_VALUE;
                break;
            }
        }
        return flag?(int)-sum:(int)sum;
    }

    public static void main1(String[] args) {
        String str = " abc ";
        System.out.println(str.length());
        str = str.trim();
        System.out.println(str.length());
    }
}
